package com.nuc.thread.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author 诗酒趁年华
 * @version 1.0.0
 * @Description 任务调度线程池
 * @createTime 2021年08月13日 16:53:00
 */
@Slf4j
public class Test3 {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
        log.debug("cheduledThreadPool");

        //轮询调度
        method1(pool);

    }

    private static void method2(ScheduledExecutorService pool) {
        pool.scheduleAtFixedRate(()->{
            log.debug("running..");
        },1,1, TimeUnit.SECONDS);
    }

    //延时调度
    private static void method1(ScheduledExecutorService pool) {
        pool.schedule(()->{
            log.debug("task1");
            try {
                int i = 1 / 0;
            } catch (Exception e) {
                log.error("出现异常：" ,e);
            }
        },1, TimeUnit.SECONDS);

        pool.schedule(()->{
            log.debug("task2");
        },1, TimeUnit.SECONDS);
    }
}
